package adb.group.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.DrugOrder;
import beans.OperationOrder;
import beans.OutPatient;

/**
 * Servlet implementation class ViewOrderServlet
 */
public class ViewOrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewOrderServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection connection = (Connection) DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:ORCL", "dbfp_dba",
					"dbfpdba");
			Statement statement = (Statement) connection.createStatement();

			// OutPatient Table
			ResultSet outPatientResult = statement
					.executeQuery("select * from outpatient_t where patient_ID='"+request.getSession().getAttribute("workID")+"'");
			ArrayList<OutPatient> outPatientList = new ArrayList<OutPatient>();
			if (outPatientResult == null) {
				response.sendRedirect("Index.jsp ");
			} else {
				while (outPatientResult.next()) {
					OutPatient outPatient = new OutPatient();
					outPatient.setPatientID(outPatientResult.getString(1));
					outPatient.setVisit_date(outPatientResult.getDate(2));
					outPatient.setVisit_fee(Integer.parseInt(outPatientResult
							.getString(3)));
					outPatientList.add(outPatient);
				}
				request.getSession().setAttribute("outPatientList", outPatientList);
			}

			// Drug Orders
			ResultSet drugResult = statement
					.executeQuery("select * from drug_order_t where patient_ID='"+request.getSession().getAttribute("workID")+"'");
			ArrayList<DrugOrder> drugOrderList = new ArrayList<DrugOrder>();

			while (drugResult.next()) {
				DrugOrder drugOrder = new DrugOrder();
				drugOrder.setPatientID(drugResult.getString(6));
				drugOrder.setOrderID(drugResult.getString(1));
				drugOrder.setDate(drugResult.getDate(2));
				drugOrder.setTreatmentID(drugResult.getString(3));
				drugOrder.setDrugID(drugResult.getString(4));
				drugOrder
						.setQuantity(Integer.parseInt(drugResult.getString(5)));
				drugOrderList.add(drugOrder);
			}
			request.getSession().setAttribute("drugOrderList", drugOrderList);

			// Operation Orders
			ResultSet operationResult = statement
					.executeQuery("select * from operation_lab_order_t where patient_ID='"+request.getSession().getAttribute("workID")+"'");
			ArrayList<OperationOrder> operationOrderList = new ArrayList<OperationOrder>();

			while (operationResult.next()) {
				OperationOrder operationOrder = new OperationOrder();
				operationOrder.setPatientID(operationResult.getString(5));
				operationOrder.setOrderID(operationResult.getString(1));
				operationOrder.setDate(operationResult.getDate(2));
				operationOrder.setTreatmentID(operationResult.getString(3));
				operationOrder.setLabID(operationResult.getString(4));
				operationOrderList.add(operationOrder);
			}
			request.getSession().setAttribute("operationOrderList", operationOrderList);
			
			//Forward
			response.sendRedirect("/project/resources/patient/viewPurchaseOrder.jsp");

		} catch (Exception e) {
			System.out.print(e);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
